package com.rockxin.cat.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.rockxin.cat.R;
import com.rockxin.cat.adapter.LendRecordAdapter;
import com.rockxin.cat.base.BaseFragment;
import com.rockxin.cat.base.Constant;
import com.rockxin.cat.base.User;
import com.rockxin.cat.bean.LendRecordBean;
import com.rockxin.cat.network.CatNetWork;
import com.rockxin.cat.network.HttpResult;
import com.rockxin.cat.utils.SPUtils;

import java.util.ArrayList;
import java.util.List;

import rx.Subscriber;

/**
 * A simple {@link Fragment} subclass.
 * 出借记录列表
 */
public class LendRecordFragment extends BaseFragment {

    private LendRecordAdapter mLendRecordAdapter;
    private List<LendRecordBean> lendRecordBeanList;
    private LendRecordBean mLendRecordBean;             //传过来的值
    private int type;  //1：还款中，2：已还清  3：募集中

    public LendRecordFragment() {
    }

    public static LendRecordFragment newInstance(LendRecordBean lendRecordBean, int type) {
        Bundle args = new Bundle();
        args.putInt("type", type);
        args.putParcelable("LendRecordBean", lendRecordBean);
        LendRecordFragment fragment = new LendRecordFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getInt("type");
            mLendRecordBean = getArguments().getParcelable("LendRecordBean");
            lendRecordBeanList = new ArrayList<>();
            SPUtils spUtils = new SPUtils("Guide");
            String token = spUtils.getString(Constant.PREFERENCES.TOKEN);
            CatNetWork catNetWork = new CatNetWork();
            catNetWork.myBorrow(type, token, 1, new Subscriber<HttpResult<List<LendRecordBean>>>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(HttpResult<List<LendRecordBean>> listHttpResult) {
                    final List<LendRecordBean> data = listHttpResult.getData();
                    for (int i = 0; i < data.size(); i++) {
                        final LendRecordBean lendRecordBean1 = data.get(i);
                        lendRecordBean1.setNum(i + "");
                    }
                    lendRecordBeanList.addAll(data);
                    mLendRecordAdapter.notifyDataSetChanged();
                }
            });
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View mView = inflater.inflate(R.layout.fragment_lend_record, container, false);
        initView(mView);
        return mView;
    }

    private void initView(View mView) {
        RecyclerView recyclerView = mView.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        mLendRecordAdapter = new LendRecordAdapter(lendRecordBeanList);
        recyclerView.setAdapter(mLendRecordAdapter);

        initHeader();
    }

    private void initHeader() {
        final View headerView = View.inflate(mContext, R.layout.fragment_capital_log_header, null);
        TextView title1 = headerView.findViewById(R.id.title1);
        TextView title2 = headerView.findViewById(R.id.title2);
        TextView title3 = headerView.findViewById(R.id.title3);
        TextView title4 = headerView.findViewById(R.id.title4);
        title1.setText(mLendRecordBean.getNum());
        title2.setText(mLendRecordBean.getName());
        title3.setText(mLendRecordBean.getRepay_money());
        title4.setText(mLendRecordBean.getRepay_time());
        mLendRecordAdapter.addHeaderView(headerView);
    }

}
